<?php
namespace backend\controllers;

use common\collection\Common;
use Yii;
use yii\web\Controller;
use common\collection\BackendCommon;
use common\helpers\PublicFun;
use backend\config\routeParams;


/**
 * Site controller
 */
class PromoteController extends Controller
{

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    public  function beforeAction($action){
        if(BackendCommon::init()){
            if(BackendCommon::rbac()){
                return true;
            }else{
                Yii::$app->response->redirect( ['site/error']);
            }
        }else{
            Yii::$app->response->redirect( ['login/index']);
        }
    }
    //创建推广
    public function actionIndex()
    {
        return $this->render('index');
    }
    //创建推广
    public function actionMypromote()
    {
        return $this->render('mypromote');
    }
    //销售推广
    public function actionSales()
    {
        //数据过滤
        $JsonData = [
            'MytipId' => Common::GetMytipId(),
            'AdType' =>100,
        ];
        $url = routeParams::HttpUrl('AdvertorialInitDataGet');
        $result = PublicFun::PostPackage($JsonData, $url);
        $result = $result = json_decode($result['Body'],true);
//        var_dump($result);
//        exit;
        return $this->render('sales',array('data' => $result['Model']));
    }
    //预约推广
    public function actionReservation()
    {
        //数据过滤
        $JsonData = [
            'MytipId' => Common::GetMytipId(),
            'AdType' =>200,
        ];
        $url = routeParams::HttpUrl('AdvertorialInitDataGet');
        $result = PublicFun::PostPackage($JsonData, $url);
        $result = $result = json_decode($result['Body'],true);
        return $this->render('reservation',array('data' => $result['Model']));

    }
     //预约推广
    public function actionReservation1()
    {
        //数据过滤
        $JsonData = [
            'MytipId' => Common::GetMytipId(),
            'AdType' =>200,
        ];
        $url = routeParams::HttpUrl('AdvertorialInitDataGet');
        $result = PublicFun::PostPackage($JsonData, $url);
        $result = $result = json_decode($result['Body'],true);
        return $this->render('reservation1',array('data' => $result['Model']));

    }
     //预约推广
    public function actionReservation2()
    {
        //数据过滤
        $JsonData = [
            'MytipId' => Common::GetMytipId(),
            'AdType' =>200,
        ];
        $url = routeParams::HttpUrl('AdvertorialInitDataGet');
        $result = PublicFun::PostPackage($JsonData, $url);
        $result = $result = json_decode($result['Body'],true);
        return $this->render('reservation2',array('data' => $result['Model']));

    }



    //活动推广
    public function actionActivity()
    {

        //数据过滤
        $JsonData = [
            'MytipId' => Common::GetMytipId(),
            'AdType' =>300,
        ];
        $url = routeParams::HttpUrl('AdvertorialInitDataGet');
        $result = PublicFun::PostPackage($JsonData, $url);
        $result = $result = json_decode($result['Body'],true);
//        var_dump($result);
//        exit;
        return $this->render('activity',array('data' => $result['Model']));
    }
    //重置密码
    public function actionInitDataGet()
    {
        $JsonData = Yii::$app->request->post();
        //数据过滤
        $JsonData = Common::ArrayFilter($JsonData);
        $JsonData['MytipId'] =  Common::GetMytipId();
        $url = routeParams::HttpUrl('AdvertorialInitDataGet');
        $result = PublicFun::PostPackage($JsonData, $url);
        $result = $result = json_decode($result['Body'],true);
        die(json_encode($result));
    }

    //创建销售活动
    public function actionAdMarketCreate()
    {
        $JsonData = Yii::$app->request->post();
        //数据过滤
        $JsonData = Common::ArrayFilter($JsonData);
        $JsonData['MytipId'] =  Common::GetMytipId();
        $url = routeParams::HttpUrl('AdMarketCreate');
        $result = PublicFun::PostPackage($JsonData, $url);
        $result = $result = json_decode($result['Body'],true);
        die(json_encode($result));
    }
    //创建报名活动
    public function actionAdActivityCreate()
    {
        $JsonData = Yii::$app->request->post();
        //数据过滤
        $JsonData = Common::ArrayFilter($JsonData);
        $JsonData['MytipId'] =  Common::GetMytipId();
        $url = routeParams::HttpUrl('AdActivityCreate');
        $result = PublicFun::PostPackage($JsonData, $url);
        $result = $result = json_decode($result['Body'],true);
        die(json_encode($result));
    }
    //创建预约活动
    public function actionAdSubscribeCreate()
    {
        $JsonData = Yii::$app->request->post();
        //数据过滤
        $JsonData = Common::ArrayFilter($JsonData);
        $JsonData['MytipId'] =  Common::GetMytipId();
        $url = routeParams::HttpUrl('AdSubscribeCreate');
        $result = PublicFun::PostPackage($JsonData, $url);
        $result = $result = json_decode($result['Body'],true);
        die(json_encode($result));
    }

    //获取软文详情
    public function actionAdTemplateGet()
    {
        $JsonData = Yii::$app->request->post();
        //数据过滤
        $JsonData = Common::ArrayFilter($JsonData);
        $url = routeParams::HttpUrl('AdTemplateGetDetail');
        $result = PublicFun::PostPackage($JsonData, $url);
        $result = $result = json_decode($result['Body'],true);
        $result['Model']['AdContent'] = stripslashes($result['Model']['AdContent']);
        die(json_encode($result));
    }

    //发布软文
    public function actionAdPublish()
    {
        $JsonData = Yii::$app->request->post();
        //数据过滤
        $JsonData = Common::ArrayFilter($JsonData);
        $JsonData['MytipId'] =  Common::GetMytipId();
        $url = routeParams::HttpUrl('AdvertorialPublish');
        $result = PublicFun::PostPackage($JsonData, $url);
        $result = $result = json_decode($result['Body'],true);
        die(json_encode($result));
    }
    //发布软文
    public function actionAdDel()
    {
        $JsonData = Yii::$app->request->post();
        //数据过滤
        $JsonData = Common::ArrayFilter($JsonData);
        $JsonData['MytipId'] =  Common::GetMytipId();
        $url = routeParams::HttpUrl('AdvertoriaDelete');
        $result = PublicFun::PostPackage($JsonData, $url);
        $result = $result = json_decode($result['Body'],true);
        die(json_encode($result));
    }
    //获取我的软文列表
    public function actionGetMyList()
    {
        $datapost = Yii::$app->request->post();
        $datapost = Common::ArrayFilter($datapost);
        $admin =  Common::GetMytipId();
        $datapost['LoginMytipId'] = $admin;
        $url = routeParams::HttpUrl('MyAdvertoerialGetList');
        $result = PublicFun::PostPackage($datapost, $url);
        $result = json_decode($result['Body'],true);
        //var_dump($result);exit;
        $ret = [
            'PageNo' => $datapost['PageNo'],
            'PageSize' => $datapost["PageSize"],
            'total' => $result["TotalCounts"],
            'data' => $result['Models']
        ];
        die(json_encode($ret));
    }

    //二进制流转化为图片
    public function actionGetBytesByImagePath(){
        $JsonData = Yii::$app->request->post();
        $JsonData = Common::ArrayFilter($JsonData);
        $base_img = $JsonData['baseImg'];
        $base_img = str_replace('data:image/jpg;base64,', '', $base_img);
        $base_img = str_replace('data:image/jpeg;base64,', '', $base_img);
        $base_img = str_replace('data:image/png;base64,', '', $base_img);
        //  设置文件路径和文件前缀名称
        $path = "./upload/Extension/";
        $prefix='nx_';
        $output_file = $prefix.time().rand(100,999).'.jpg';
        $path = $path.$output_file;
        //  创建将数据流文件写入我们创建的文件内容中
        $ifp = fopen( $path, "wb" );
        if( fwrite( $ifp, base64_decode( $base_img))){
            fclose( $ifp );
            $file = file_get_contents(Yii::$app->urlManager->hostInfo.'/upload/Extension/'.$output_file);
            $datalist = base64_encode($file);
            $Data = array(
                "Files" => $datalist,
                "EnumImgSuffix" => 0,
                "EnumPathName" => 9,
            );
            $url = routeParams::HttpUrl('ImageUpload');
            $result = PublicFun::PostPackage($Data, $url);
            $result = json_decode($result['Body'],true);
            if ($result['IsBizSuccess']) {
                $Photo =routeParams::HttpUrl('IMGURL') . "/" . $result['FileName'];
                $result['ImgSrc'] =  $Photo;
                die(json_encode($result));
            }else{
                $info = array(
                    'IsBizSuccess' => false,
                    'ErrMsg' => '图片上传失败！'
                );
                die(json_encode($info));
            }

        }else{
            fclose( $ifp );
            $result =  [
                'IsBizSuccess'=>false,
                'ErrMsg'=>'图片上传失败！',
            ];
            die(json_encode($result));
        }
    }

}
